/****************************************************************************
 *  XHR
 *	created by Simone Cingano (2.0)
 ****************************************************************************/

// var xhrReq = [];

/* XHR // Send Request
 *   @url       url requested
 *   @target    target dom id (its content will be overwritted)
 *   @postdata  postdata (if present request will be POST instead of GET)
 *   @loading   temporary content of target while xhr is waiting for response
 *   @starts    some html inserted at target's content start 
 *   @done      some javascript code to be evaluated if 
 *
 *//*
function xhrRequest(kw) { //(url, target, postdata, loading, starts, done) {
  // Clean resources
  idx = xhrReq.length-1;
  while (xhrReq[idx]==null) { xhrReq.pop(); if (--idx < 0) break; }
  // Create request
  var t = document.getElementById(kw.target); if (t) { t.innerHTML = kw.loading; }
  if (window.XMLHttpRequest) newrequest = new XMLHttpRequest();
  else if (window.ActiveXObject) newrequest = new ActiveXObject("Microsoft.XMLHTTP");
  else { alert('Unable to instance an XMLHttpRequest Object'); return; }
  var xhrReqIndex = parseInt(xhrReq.push(newrequest)-1);
  kw.xhrReqIndex = xhrReqIndex
  req = xhrReq[xhrReqIndex];
  // Send request
  if (req != undefined) {
    if (!kw.sync) req.onreadystatechange = function() { xhrResponse(kw.url, kw.target, kw.xhrReqIndex, kw.loading, kw.starts, kw.done); };
	if (kw.postdata) {
        // POST request
		req.open("POST", kw.url, true);
		req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		req.setRequestHeader("Connection","close"); 
		req.send(kw.postdata);
	} else {
        // GET request
		req.open("GET", kw.url, true);
		req.send("");
	}
    if (kw.sync) {
        return xhrResponse(kw.url, kw.target, kw.xhrReqIndex, kw.loading, kw.starts, kw.done);
    }
  }
}
*/
/* xhrDONE // Receive Response
 *   @params     receive request's params
 *
 *   REDIRECT: when response starts with "redirect>" send a new request using the given url
 *   example of redirect in responseText: "redirect>mypage.asp?id=5"
 */
/*function xhrResponse(kw) { //(url, target, index, loading, starts, done) {
  req = xhrReq[kw.xhrReqIndex];
  if (req.readyState == 4) {
    try { t = document.getElementById(target).innerHTML; }
    catch(e) { t = ''; }
    if (req.status == 200) {
        kwargs = kw; kwargs.url = req.responseText.substring(9); kwargs.postdata = '';
        if (req.responseText.substring(0,9)=='redirect>') xhrRequest(kwargs);
        else { t = starts + req.responseText; }
    } else {
        t = starts + "<b>xhr error:</b><br/>\n"+req.statusText+'\n<hr/>'+req.responseText;
    }
    if (done!='') eval(done);
    xhrReq[index] = null; //destroy object
  }
  return t
}
*/
